<?php

session_start();
require_once('connect.php');
require_once('includes/user.class.php');
require_once('includes/teachers.class.php');

$database = Database::getInstance();
$teachers = new Teachers();
$user = new User();

if(!$user->is_logged()) header('Location: index.php');
require_once('partials/header.php'); //wczytaj header i menu
?>


    <div class="container">

        <div class="row">

            <div class="col-md-3">

                <?php require_once('partials/content-left-menu.php'); ?>

            </div>

            <div class="col-md-9">
				<div class="subject-table">
					<div class="subject-table-header subject-table-row">
						<div class="subject-table-header-cell">Imię</div>
						<div class="subject-table-header-cell">Nazwisko</div>
						<div class="subject-table-header-cell">email</div>
					</div>
					
					<?php
					$teachersData = $teachers -> get_teachers();
					foreach($teachersData as $teacher){
						print '<div class="subject-table-row">';
						print'<div class="subject-table-header-cell">'.$teacher['firstName'].'</div>';
						print'<div class="subject-table-header-cell">'.$teacher['lastName'].'</div>';
						print'<div class="subject-table-header-cell">'.$teacher['mail'].'</div>';
						print '</div>';
					}
					?>
				</div>
				<!--TYLKO DLA ADMINOW -->
				<?php
                if($user->is_leader()){
                    if(isset($_GET['addTeacher'])){
                        $firstName = $_GET['firstName'];
                        $lastName = $_GET['lastName'];
                        $mail = $_GET['mail'];

                        if($teachers->check_teacher($firstName,$lastName,$mail)){
                            $add = $teachers->add_teacher($firstName,$lastName,$mail);
                            if($add > 0){
                                header( 'Location:teachers.php');
                            }else{
                                $alert = array('Nie dodano nauczyciela', 'danger');
                            }
                        }else{
                            $alert = array('Taki nauczyciel już istnieje', 'success');
                        }
                        $user->show_alert($alert);
                    }
                    ?>
                    <div class="add-news-subject-area">
                        <form action = "teachers.php" method = "GET">
                            <input type="text" name="firstName" class="subject-input" placeholder="Imię">
                            <input type="text" name="lastName" class="subject-input" placeholder="Nazwisko">
                            <input type="text" name="mail" class="subject-input" placeholder="email">
                            <input type="hidden" name="addTeacher" value="true">
                            <input type="submit" name="save_teacher" value="Zapisz prowadzącego w bazie" class="subject-button">
                        </form>
                    </div>
                    <!--TYLKO DLA ADMINOW -->
                <?php
                }
                ?>
            </div>

        </div>

    </div>

 
<?php
require_once('partials/footer.php');
?>
